package com.herui.ota.controller;

import com.herui.ota.config.ControllerNames;
import io.swagger.annotations.Api;
import org.shan.spring.base.annotations.SysLog;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

@Api(value = "图片接口", tags = {"图片接口"})
@SysLog(title = "图片")
@RestController
@RequestMapping("/image")
public class ImageController {

    private static final String UPLOAD_DIR = "uploads/";

    @PostMapping("/upload")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            // 保存上传的文件
            String fileName = file.getOriginalFilename();
            File destFile = new File(UPLOAD_DIR + fileName);
            file.transferTo(destFile);
            return ResponseEntity.ok("File uploaded successfully: " + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to upload file: " + e.getMessage());
        }
    }

    @PostMapping("/generateThumbnail")
    public ResponseEntity<byte[]> generateThumbnail(@RequestParam("file") MultipartFile file) {
        try {
            // 将MultipartFile转换为InputStream
            InputStream inputStream = file.getInputStream();

            // 将InputStream转换为BufferedImage
            BufferedImage originalImage = ImageIO.read(inputStream);

            // 创建缩略图
            int thumbnailWidth = 100; // 缩略图宽度
            int thumbnailHeight = 100; // 缩略图高度
            BufferedImage thumbnail = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = thumbnail.createGraphics();
            graphics2D.drawImage(originalImage, 0, 0, thumbnailWidth, thumbnailHeight, null);
            graphics2D.dispose();

            // 将缩略图保存到字节数组
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(thumbnail, "jpg", outputStream);
            byte[] thumbnailBytes = outputStream.toByteArray();

            // 关闭输入流
            inputStream.close();

            // 返回生成的缩略图字节数组
            return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=thumbnail.jpg").body(thumbnailBytes);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(("Error generating thumbnail: " + e.getMessage()).getBytes());
        }
    }

    @GetMapping("/download/{fileName}")
    public ResponseEntity<byte[]> downloadFile(@PathVariable String fileName) {
        try {
            // 加载文件
            File file = new File(UPLOAD_DIR + fileName);

            // 读取文件内容
            byte[] fileContent = new byte[(int) file.length()];
            FileInputStream inputStream = new FileInputStream(file);
            inputStream.read(fileContent);
            inputStream.close();

            // 构建响应
            return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=" + fileName).body(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(("Error downloading file: " + e.getMessage()).getBytes());
        }
    }
}
